<?php
/**
 * 
 * controller for module[index]
 * ThankPHP framework
 * 
 * @author etongchina <etongchina@gmail.com>
 * @versioin 0.1
 */

/**
 * controller for module[index]
 *
 * @package controller_index
 */
class controller_index extends controller{
	public $vo = array();
	
	/**
	 * set the vo object
	 * 
	 * @param string $key
	 * @param string $value
	 */
	function setVo($key, $value){
		$this->vo[$key] = $value;
	}
	
	/**
	 * action[index]
	 * 
	 * @param void
	 */
	function index(){
		global $DEBUG,$DIR;
		//when action index exec
		if($DEBUG)
		print "when action index exec<br/>";
		$sql = "select * from bookstore";
		$handle = $this->model->query($sql);
		$bookstore = $this->model->fetchArray($handle);
		$this->setVo('bookstore', $bookstore);
		$this->setVo("DIR", $DIR);
		$this->setTpl("index");
	}
	
	/**
	 * action[add]
	 * 
	 * @global $DEBUG
	 * @param void
	 */
	function add(){
		global $DEBUG;
		//when action index exec
		if($DEBUG)
		print "when action add exec<br/>";
		$pv = array('bookname', 'author', 'price', 'num');
		$insert = array();
		foreach($_POST AS $k=>$v){
			if(in_array($k, $pv)){
				$insert[$k] = $v;
			}
		}
		unset($_POST);
		$this->model->insert("bookstore", $insert);
		$this->urls->back('add success');
		//$this->setTpl("index");
	}
	
	/**
	 * dispatcher the controller
	 * 
	 * @param void
	 */
	function dispatcher(){
		parent::dispatcher();
	}
	
	/**
	 * get the vo object
	 * 
	 * @param void
	 * @return $this->vo
	 */
	public function getVo(){
		return $this->vo;
	}
}